package com.biao.synchronized_test.test_lock;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class Test01 implements Runnable {

    private Lock lock = new ReentrantLock();
    private static int count = 100;

    @Override
    public void run() {
        while (count > 1) {
            try {
                lock.lock();
                count--;
                System.out.println(Thread.currentThread().getName() + "---" + count);
            } finally {
                lock.unlock();
            }
        }
    }

    public static void main(String[] args) {
        Test01 test = new Test01();
        new Thread(test).start();
        new Thread(test).start();
        // 可能线程执行太快，所以另一个线程还没得到cpu资源，第一个线程就执行完了
    }

}
